package com.abc.core.util;

import com.abc.core.instance.bo.Cell;
import com.abc.core.instance.bo.Inst;
import com.abc.core.instance.bo.Va;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

public class CellSerializer extends JsonSerializer<Cell> {

    @Override
    public void serialize(Cell cell, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (cell.getOwner() == null) {
            throw new RuntimeException("Cell 不存在owner");
        }

        gen.writeStartObject();
        gen.writeNumberField("id", cell.getId());
        gen.writeStringField("operationFlag", cell.getOperationFlag());
        gen.writeNumberField("rootId", cell.getRootId());
        gen.writeNumberField("parentId", cell.getParentId());
        gen.writeStringField("dnaCode", cell.getDnaCode());

        for (Va va : cell.getVas().values()) {
            // TODO Date DateTime
            gen.writeObjectField(va.getName(), va.getValue());
        }

        for (Inst childInst : cell.getChildren().values()) {
            gen.writeObjectField(childInst.getDnaName(), childInst);
        }

        gen.writeEndObject();
    }
}
